﻿using IdentityServer4;
using IdentityServer4.Models;
using static IdentityServer4.IdentityServerConstants;

namespace Api.IdentityServer4
{
    public class Config
    {
        public static IEnumerable<IdentityResource> IdentityResources => new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile()
        };

        public static IEnumerable<ApiScope> ApiScopes => new ApiScope[]
        {
            new ApiScope("api1", "My Api1")
        };

        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource("api1", "My API")
                {  
                    // 如果你的API需要特定的作用域，可以在这里定义  
                    // Scopes = { new Scope("myapiscope") }  
                }
            };
        }

        public static IEnumerable<Client> Clients => new List<Client>
        {
            new Client
            {
                ClientId = "client_api1",
                // 要使用RefreshToken时，必须要把AllowedGrantTypes设置为授权代码、混合和资源所有者密码凭证流
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, //GrantTypes.ClientCredentials,
                ClientSecrets =
                {
                    new Secret("secret".Sha256()) //secret加密密钥 Sha256加密方式
                },
                AllowedScopes =
                {
                    "api1",
                    IdentityServerConstants.StandardScopes.OfflineAccess,
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                },
                // 刷新Token时RefreshToken保持不变
                RefreshTokenUsage = TokenUsage.ReUse,
                RefreshTokenExpiration = TokenExpiration.Sliding,
                // RefreshToken过期时间
                SlidingRefreshTokenLifetime = 3600,
                // 明确授权请求刷新令牌
                AllowOfflineAccess = true,
                // TOken过期时间
                AccessTokenLifetime = 60,
            }
        };
    }
}
